<?php


namespace App\Models\Field;

use App\Common\Libs\ArrayHelper;
use App\Consts\GlobalConst;

class ArticleField extends BaseField
{
    // 相似文章
    public const SIMILAR_ARTICLE = 'similarArticle';

    // 消息静态化
    public const MESSAGE_STATIC = 'messageStatic';

    // 笔记
    public const NOTE = 'note';

    // 动态
    public const DYNAMIC = 'dynamic';

    public static function getFormats($scene = self::BASE_INFO): array

    {
        $baseFormats = [
            'id' => static function ($model) {
                return $model['article_id'];
            },
            'author_id',
            'cover_url' => static function ($model) {
                if (strpos($model['cover_url'], 'http') !== false) {
                    return env('ALIOSS_IMG_URL') . $model['cover_url'];
                }

                return $model['cover_url'];
            },
            'title',
            'description',
            'type',
            'is_deleted',
            'is_shield',
            'created_at',
        ];
        $formats = [
            self::MESSAGE_STATIC => [
                'id' => static function ($model) {
                    return $model['article_id'];
                },
                'cover_url',
                'description',
                'type',
                'is_deleted',
                'target_type' => GlobalConst::TARGET_TYPE_ARTICLE,
                'title',
            ],
            self::SELF_DETAIL => ArrayHelper::merge($baseFormats, [
                'original_url',
            ]),
            self::SIMILAR_ARTICLE => [
                'id' => static function ($model) {
                    return $model['article_id'];
                },
                'title',
                'cover_url',
            ],
            self::NOTE => $baseFormats,
            self::DYNAMIC => $baseFormats,
        ];

        return $formats[$scene] ?? $baseFormats;
    }
}
